package com.jshcbd.modules.asset.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jshcbd.framework.vo.Result;
import com.jshcbd.framework.query.QueryGenerator;
import com.jshcbd.modules.asset.dto.ReceiveDto;
import com.jshcbd.modules.asset.dto.RenewDto;
import com.jshcbd.modules.asset.dto.RetrocedeDto;
import com.jshcbd.modules.asset.entity.ReceiveItemEntity;
import com.jshcbd.modules.asset.entity.RetrocedeItemEntity;
import com.jshcbd.modules.asset.service.IAssetReturnService;
import com.jshcbd.modules.asset.service.IAssetService;
import com.jshcbd.modules.asset.vo.AssetVo;
import com.jshcbd.modules.asset.vo.RetrocedeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhoucd
 * @date
 * @description
 */
@Slf4j
@Api(tags="资产归还")
@RestController
@RequestMapping("/retrocede")
public class AssetRetrocedeController {

    @Autowired
    private IAssetReturnService assetReturnService;

    @Autowired
    private IAssetService assetService;


    @ApiOperation("资产归还记录")
    @GetMapping(value = {"/listByMainId/{assetId}","listByMainId"})
    public Result<IPage<RetrocedeVo>> listByMainId(@PathVariable(required = false)  String assetId, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<RetrocedeVo>> result = new Result();
        Page<RetrocedeVo> page = new Page(pageNo, pageSize);
        IPage<RetrocedeVo> pageList = assetReturnService.pageViewByAssetId(page,assetId);
        result.setData(pageList);
        return result;
    }



    @ApiOperation("归还记录")
    @GetMapping("/list")
    public Result<IPage<RetrocedeVo>> list(RetrocedeVo retrocedeVo, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<RetrocedeVo>> result = new Result();
        QueryWrapper<RetrocedeVo> queryWrapper = QueryGenerator.initQueryWrapper(retrocedeVo, req.getParameterMap());
        Page<RetrocedeVo> page = new Page(pageNo, pageSize);
        IPage<RetrocedeVo> pageList = assetReturnService.pageView(page,queryWrapper);
        result.setData(pageList);
        return result;
    }



    @ApiOperation("资产归还列表")
    @GetMapping("/listAsset")
    public Result<IPage<AssetVo>> returnList(AssetVo assetVo, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                             @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req){
        Result<IPage<AssetVo>> result = new Result();
        QueryWrapper<AssetVo> queryWrapper = QueryGenerator.initQueryWrapper(assetVo, req.getParameterMap());
        queryWrapper.eq("state",2);
        Page<AssetVo> page = new Page(pageNo, pageSize);
        IPage<AssetVo> pageList = assetService.pageView(page,queryWrapper);
        result.setData(pageList);
        return result;
    }


    @ApiOperation("资产归还记录明细")
    @GetMapping("/listItemByMainId")
    public Result<IPage<AssetVo>> listItemByMainId(ReceiveItemEntity itemEntity, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<AssetVo>> result = new Result();
        IPage<AssetVo> assetVoPage = new Page(pageNo, pageSize);
        Page page = new Page(pageNo, pageSize);
        if(itemEntity.getMainId() !=null){
            IPage<RetrocedeItemEntity> pageList = assetReturnService.pageRetrocedeItemsByMainId(page,itemEntity.getMainId());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.in("id",pageList.getRecords().stream().map(rec -> rec.getAssetId()).toArray());
            assetVoPage.setTotal(pageList.getTotal());
            assetVoPage.setPages(pageList.getPages());
            assetVoPage.setRecords(assetService.listView(queryWrapper));
            result.setData(assetVoPage);
        }else{
            result.setData(assetVoPage);
        }
        return result;
    }

    //

    @ApiOperation("资产归还")
    @PostMapping("/retrocede")
    public Result retrocede(@RequestBody RetrocedeDto retrocedeDto) {
        assetReturnService.doRetrocede(retrocedeDto);
        return Result.ok("归还成功！");
    }


    @ApiOperation("资产转移")
    @PostMapping("/move")
    public Result move(@RequestBody ReceiveDto recevieDto) {
        assetReturnService.doMove(recevieDto);
        return Result.ok("转移成功！");
    }

    @ApiOperation("资产续期")
    @PostMapping("/renew")
    public Result renew(@RequestBody RenewDto renewDto) {
        assetReturnService.doRenew(renewDto);
        return Result.ok("续期成功！");
    }


}
